package com.zhangzhan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangzhan.constants.SystemConstants;
import com.zhangzhan.domain.commcon.ResponseResult;
import com.zhangzhan.domain.entity.Comment;
import com.zhangzhan.domain.enums.AppHttpCodeEnum;
import com.zhangzhan.domain.vo.CommentVO;
import com.zhangzhan.domain.vo.PageVO;
import com.zhangzhan.exception.MySystemException;
import com.zhangzhan.mapper.CommentMapper;
import com.zhangzhan.service.CommentService;
import com.zhangzhan.service.UserService;
import com.zhangzhan.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2022-09-19 20:05:43
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    UserService userService;


    //查询评论和对应根评论的子评论
    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        //先查询根评论
        Page<Comment> commentPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Comment> commentWrapper = new LambdaQueryWrapper<>();
        commentWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId);
        commentWrapper.eq(Comment::getRootId,-1);
        commentWrapper.eq(Comment::getType,commentType);
        //然后进行分页
        page(commentPage,commentWrapper);
        page(commentPage,commentWrapper);
        //转换成vo进行返回
        List<CommentVO> list = toCommentVoList(commentPage.getRecords());

        //子评论的查询
        for (CommentVO commentVO : list) {

            List<CommentVO> children = getChildrens(commentVO.getId());

            commentVO.setChildren(children);
        }

        return ResponseResult.okResult(new PageVO(list,commentPage.getTotal()));
    }


    private List<CommentVO> getChildrens(Long id) {
        //根据rootid查询子评论
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getRootId,id);
        wrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> list = list(wrapper);

        List<CommentVO> commentVOS = toCommentVoList(list);

        return commentVOS;


    }

    //封装属性字段值
    private List<CommentVO> toCommentVoList(List<Comment> list){
        List<CommentVO> commentVOS = BeanCopyUtils.beanCopyUtil(list, CommentVO.class);

        //遍历封装数据
        for (CommentVO commentVO : commentVOS) {

            if (commentVO.getCreateBy() != null){
                String nickName = userService.getById(commentVO.getCreateBy()).getNickName();
                commentVO.setUsername(nickName);
            }

            //通过toCommentUserId查询用户的昵称并赋值
            //如果toCommentUserId不为-1才进行查询
            if(commentVO.getToCommentUserId()!=-1){
                String nickName1 = userService.getById(commentVO.getToCommentUserId()).getNickName();
                commentVO.setToCommentUserName(nickName1);
            }
        }
        return commentVOS;
    }

    //新增评论
    @Override
    public ResponseResult commentSave(Comment comment) {
        if(!StringUtils.hasText(comment.getContent())){
            throw new MySystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }

        save(comment);
        return ResponseResult.okResult();
    }
}

